package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class uplodController {
    @Autowired
   private AliOssUtil aliOssUtil;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 实现图片上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
   public Result uplodimg(@RequestParam("imgFile") MultipartFile file){
        try {
            //获取uuid
            log.info("实现文件上传：{}",file);
            String key="photo";
            String originalFilename = file.getOriginalFilename();
            String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
            String s = UUID.randomUUID().toString();
            String filename=s+substring;//把uuid和上面截取的字符串进行拼接
            aliOssUtil.upload(file.getBytes(),filename);//会返回图片的字符串路径
            redisTemplate.opsForSet().add(key,aliOssUtil.upload(file.getBytes(),filename));//会返回图片的字符串路径
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,aliOssUtil.upload(file.getBytes(),filename));
        } catch (IOException e) {
            log.error(MessageConstant.PIC_UPLOAD_FAIL,e );
        }
        return null;
    }
}

